<?php

namespace bc\model\unsubscribe;

use bc\ar\MActiveRecord;
use bc\model\wx\User;

class LogModel extends MActiveRecord
{
	public static function tableName()
	{
		return '{{%unsubscribe_log}}';
	}
	
	
	
	/**
	 * 获取统计列表使用的数据
	 * @param $start_time
	 * @param $end_time
	 *
	 */
	public function getStatistics($start_time, $end_time)
	{
		$start_time_str = strtotime($start_time);
		$end_time_str = strtotime($end_time);
		$sql = "select FROM_UNIXTIME(unsubscribe_time, '%Y-%m-%d') as `date`, count(unsubscribe_time)  as total from bc_unsubscribe_log where unsubscribe_time >={$start_time_str} and unsubscribe_time <= {$end_time_str} group by unsubscribe_time order by unsubscribe_time ASC";
		return $this->getDb()->createCommand($sql)->queryAll();
	}
	
	
	/**
	 * 
	 * @param array | string $params
	 * @param int $page
	 * @param int $pageSize
	 */
	public function getListJoinUser($params, $page = null, $pageSize = null)
	{
		$ar = $this->find ()->select ( '*' )
		->leftJoin ( 'wxch_user', self::getTableSchema ()->name . '.openid= wxch_user.wxid' );
		if ($params)
		{
			$ar->where ( $params );
		}
		if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset ( $offset )->limit ( $pageSize );
		}
	
		if ($this->sort != null)
			$ar->orderBy ( $this->sort );
		return $ar->asArray ()->all ();
	}
}

